{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 登录注册\n",
    "- 1、服务器启动后给用户提示进行任务选择\n",
    "- 2、要用就保留用户的账号密码\n",
    "- 3、用户登陆时可以增加一个验证码的功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "bar = '''\n",
    "    欢迎来到xxx网站\n",
    "1、用户登录\n",
    "2、用户注册\n",
    "3、退出\n",
    "'''\n",
    "# if [ ! -f ./user.json ]\n",
    "if not os.path.isfile(\"./user.json\"):\n",
    "    with open(\"user.json\", \"w\") as f:\n",
    "        f.write(\"[]\")\n",
    "\n",
    "\n",
    "while True:\n",
    "    print(bar)\n",
    "    result = input(\"请输入任务编号：\")\n",
    "    if result == \"1\":\n",
    "        with open(\"user.json\", \"r\") as f:\n",
    "            userdata = json.load(f)\n",
    "        user, passwd = input(\"请输入你的用户名：\"), input(\"请输入你的密码：\")\n",
    "        r = [x for x in userdata if x[\"用户名\"] == user and x[\"密码\"] == passwd]\n",
    "        if r:\n",
    "            print(f\"恭喜，登录成功\")\n",
    "            print(r)\n",
    "        else:\n",
    "            print(\"登录失败\")\n",
    "    elif result == \"2\":\n",
    "        with open(\"user.json\", \"r\") as f:\n",
    "            userdata = json.load(f)  \n",
    "        # 需要获取用户的哪些信息\n",
    "        data = [\"用户名\", \"密码\", \"邮箱\"]\n",
    "        # 将上面的信息初始化为一个字典\n",
    "        d = dict.fromkeys(data)\n",
    "        # 循环取字典中的每一个键，并且让用户填充信息\n",
    "        for i in d:\n",
    "            d[i] = input(f\"请输入你的{i}:\")\n",
    "        with open(\"user.json\", \"w\") as f:\n",
    "            userdata.append(d)\n",
    "            json.dump(userdata, f, indent=\"\\t\", ensure_ascii=False)\n",
    "        print(\"注册成功\")\n",
    "    elif result == \"3\":\n",
    "        break\n",
    "    else:\n",
    "        print(\"任务不存在\")\n",
    "print(\"程序结束\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = [0,None,False,[],()]\n",
    "print(all(a))\n",
    "print(any(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数的定义\n",
    "```python\n",
    "def func(x):\n",
    "    print(x)\n",
    "    return x\n",
    "def 定义函数的关键字\n",
    "func 函数的名字\n",
    "(x) 函数的参数\n",
    "print(x) 函数体\n",
    "return 函数的返回值\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "121\n"
     ]
    }
   ],
   "source": [
    "def mysum(x):\n",
    "    return sum(x)\n",
    "\n",
    "# 函数调用\n",
    "data = mysum([55,33,22,11])\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100, 20, 5.0)\n"
     ]
    }
   ],
   "source": [
    "def func(x, y):\n",
    "    return x ** y, x * y, x / y\n",
    "\n",
    "data = func(10, 2)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e10adc3949ba59abbe56e057f20f883e\n"
     ]
    }
   ],
   "source": [
    "## 密码加密\n",
    "import hashlib\n",
    "def hash_password(password):\n",
    "    p = hashlib.md5(password.encode(encoding=\"utf-8\"))\n",
    "    return p.hexdigest()\n",
    "\n",
    "a = hash_password(\"123456\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数的参数\n",
    "- 在函数定义时()中的参数，叫做形参，形式参数\n",
    "  - 在形参中，带有=的参数叫做默认参数\n",
    "  - 不带=的参数叫做位置参数或普通参数\n",
    "  - 默认参数要写在后面\n",
    "- 在函数调用时()中的参数，叫做实参，实际参数\n",
    "  - 在实参中，带有=的参数叫做关键字参数\n",
    "  - 不带=的参数叫做位置参数或普通参数\n",
    "  - 关键字参数写在后面\n",
    "```python\n",
    "def func(x,y):\n",
    "    return sum(x,y)\n",
    "\n",
    "func(10,5)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫校长, 我今年22岁\n"
     ]
    }
   ],
   "source": [
    "def func(name, age=18):\n",
    "    print(f\"我叫{name}, 我今年{age}岁\")\n",
    "\n",
    "func(\"校长\", age=22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫小张, 我今年18岁\n",
      "(51, 'ggagg', 565, 333) {'a': 5, 'b': 3}\n"
     ]
    }
   ],
   "source": [
    "## 动态参数或万能参数\n",
    "def func(name, *args ,age=18, **kwargs):\n",
    "    print(f\"我叫{name}, 我今年{age}岁\")\n",
    "    print(args,kwargs)\n",
    "\n",
    "func(\"小张\",51,\"ggagg\",565,333, age=18, a=5,b=3)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess\n",
    "# 监控1 分钟的cpu负载\n",
    "def cpuload():\n",
    "    cpu_data = subprocess.getoutput('''uptime | awk '{print $(NF-2)}' | tr -d \",\"''')\n",
    "    cpu_data = float(cpu_data) * 100\n",
    "    cpus = subprocess.getoutput(\"\"\"lscpu  | grep '^CPU(s):' | awk '{print $2}'\"\"\")\n",
    "    cpus = int(cpus)\n",
    "    if cpu_data / cpus > 100:\n",
    "        print(\"cpu 负载过高\")\n",
    "\n",
    "# 监控内存使用百分比\n",
    "def free():\n",
    "    total,free = subprocess.getoutput(\"\"\"free -m | awk 'NR==2{print $2,$4}'\"\"\").split(\" \")\n",
    "    total,free = int(total),int(free)\n",
    "    if free / total * 100 < 98:\n",
    "        print(free / total * 100)\n",
    "        print(\"内存剩余空间不足\")\n",
    "# 监控当前/分的的剩余空间\n",
    "\n",
    "free()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
